﻿using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Wms.IServices;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Asn1.Crmf;

namespace Estsh.Core.Web.Plugin.Wms.Areas.wms.Controllers.WorkReport
{
	[Route("wms/[controller]")]
	[ApiController]
	public class ProductionReportingController : Controller
	{
		private IProductionReportingService service;

		// GET: LoginController
		public ProductionReportingController(IProductionReportingService _service)
		{
			service = _service;
		}

		[HttpPost("index")]
		public IActionResult Index()
		{
			return View();
		}

		/// <summary>
		///查询工作中心（产线）
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetSysPdlineList")]
		public IActionResult GetSysPdlineList(Newtonsoft.Json.Linq.JObject jobj)
		{
			string userId = jobj["userId"].ToString().Trim();
			//string factoryId = jobj["factoryId"].ToString().Trim();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				List<Syspdline_Extend> obj = service.GetSysPdlineList(userId);
				if (obj != null)
				{
					result.Success = true;
					result.Data = obj;
				}
				else
				{
					result.Success = true;
				}
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

		/// <summary>
		///根据工作中心查询工单
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetWorkOrderByPdlineId")]
		public IActionResult GetWorkOrderByPdlineId(Newtonsoft.Json.Linq.JObject jobj)
		{
			string pdlineId = jobj["pdline"].ToString().Trim();
			//string factoryId = jobj["factoryId"].ToString().Trim();
			WmsResponseResult result = new WmsResponseResult();
			try
			{
				List<MesWorkOrder_Extend> obj = service.GetWorkOrderByPdlineId(pdlineId);
				if (obj.Count>0)
				{
					result.Success = true;
					result.Data = obj;
				}
				else
				{
					result.Msg = "产线下没有工单数据！";
					result.Success = true;

				}
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

		/// <summary>
		///根据工单、产线来查询 
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetEmPdlineByOrderNoAndPdline")]
		public IActionResult GetEmPdlineByOrderNoAndPdline(Newtonsoft.Json.Linq.JObject jobj)
		{
			string pdlineId = jobj["pdlineId"].ToString().Trim();
			string orderNo = jobj["orderNo"].ToString().Trim();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetEmPdlineByOrderNoAndPdline( pdlineId, orderNo);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);

		}

		/// <summary>
		///根据工单、产线来查询 
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetWorkOrderByPdlineIdAndOrderNo")]
		public IActionResult GetWorkOrderByPdlineIdAndOrderNo(Newtonsoft.Json.Linq.JObject jobj)
		{
			string pdlineId = jobj["pdlineId"].ToString().Trim();
			string orderNo = jobj["orderNo"].ToString().Trim();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetWorkOrderByPdlineIdAndOrderNo(pdlineId, orderNo);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);

		}



		/// <summary>
		///保存人员与产线的关系
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("SaveEmpByPdline")]
		public IActionResult SaveEmpByPdline(Newtonsoft.Json.Linq.JObject jobj)
		{
			string empNo = jobj["empNo"].ToString().Trim();
			string pdlineId = jobj["pdlineId"].ToString().Trim();
			string orderNo = jobj["orderNo"].ToString().Trim();
			string loginId = jobj["loginId"].ToString().Trim();
			//string factoryId = jobj["factoryId"].ToString().Trim();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.SaveEmpByPdline(empNo, pdlineId, orderNo, loginId);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);

		}

		/// <summary>
		///根据工单零件判断是否是成品
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetworkOrderByPartType")]
		public IActionResult GetworkOrderByPartType(Newtonsoft.Json.Linq.JObject jobj)
		{
			string partId = jobj["partId"].ToString().Trim();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetworkOrderByPartType(partId);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);

		}


		/// <summary>
		///根据工单零件判断是否是成品
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetSysStockByCartonNo")]
		public IActionResult GetSysStockByCartonNo(Newtonsoft.Json.Linq.JObject jobj)
		{
			string cartonNo = jobj["turnoverBox"].ToString().Trim();
			string partNo = jobj["partNo"].ToString().Trim();
			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetSysStockByCartonNo(cartonNo, partNo);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

		/// <summary>
		///验证模具号和生成报工数据
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("SaveModelNoByWorkReport")]
		public IActionResult SaveModelNoByWorkReport(Newtonsoft.Json.Linq.JObject jobj)
		{
			string modelNo = jobj["modelNo"].ToString().Trim();//模具号
			string cartonNo = jobj["cartonNo"].ToString().Trim();//周转箱
			string Num = jobj["Num"].ToString().Trim();//下线数量
			string pdlineId = jobj["pdlineId"].ToString().Trim();//产线
			string orderNo = jobj["orderNo"].ToString().Trim();////工单号
			string loginId = jobj["loginId"].ToString().Trim();//操作人
		    string factoryId = jobj["factoryId"].ToString().Trim();//工厂id

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.SaveModelNoByWorkReport(modelNo, cartonNo, Num, pdlineId,orderNo, loginId, factoryId);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);

		}





	}

}
